<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;
use wechat\Wechat;
class UserIndex extends Api
{
    //用户端-获取最新用户数据
    public function read()
    {
        $this->checkLogin();
        urladd($this->userinfo);
        return datasuccess($this->userinfo);
    }
    //授权登录
    public function login() {
        $postData = array_filter(request()->post());
        // $validate = new \app\api\validate\LoginToken();
        // if ($validate->check($postData)) {
        if ($postData) {
            //是否有上级id
            if(isset($postData['uid']) && !empty($postData['uid'])){
                $uid = $postData['uid'];
                unset($postData['uid']);
            }
            //通过微信授权取得微信openid;
            $wechat = Wechat::instance($this->appid, $this->appsecret);
            $miniprogram = $wechat->miniprogram();
            $accessToken = random(32);
            $res = $miniprogram->code2session($postData['wxcode']);
            if ($res) {
                $userModel = app('app\api\model\UserIndex')->usercheck($miniprogram->openid, $accessToken,$uid ?? '');
                if (empty($userModel)) {
                    return msgerror("用户信息入库失败");
                }
            } else {
                return msgerror("微信接口调用失败:" . $miniprogram->getErrMsg());
            }
            //取得解密微信信息
            $la = $miniprogram->getUserinfo($postData['wxcode'], $postData['encrypted'], $postData['iv']);
            if ($la) {
                $userinfo = $miniprogram->userinfo;
                $user = $this->model->where('id',$userModel->id)->find();
                if(!empty($user) && !empty($user->toArray())){
                    $user = $user->toArray();
                    if(!empty($user['nickname'])){
                        $saveData = [
                            'image' => $userinfo['avatarUrl'],
                            'wx_data' => json_encode($userinfo),
                            'last_login_time' => date('Y-m-d H:i:s'),
                            'token' => $accessToken
                        ];
                    }else{
                        $saveData = [
                            'nickname' => $userinfo['nickName'],
                            'image' => $userinfo['avatarUrl'],
                            'wx_data' => json_encode($userinfo),
                            'last_login_time' => date('Y-m-d H:i:s'),
                            'token' => $accessToken
                        ];
                    }
                }else{
                    $saveData = [
                        'nickname' => $userinfo['nickName'],
                        'image' => $userinfo['avatarUrl'],
                        'wx_data' => json_encode($userinfo),
                        'last_login_time' => date('Y-m-d H:i:s'),
                        'token' => $accessToken
                    ];
                }
                $model = $this->model;
                $user = $this->model->where('id',$userModel->id)->find()->toArray();
                if ($model::update($saveData, ['id' => $userModel->id])) {
                    return datasuccess(['access_token' => $accessToken,'user_id'=>$userModel->id,'data'=>$user]);
                } else {
                    return msgerror("用户信息保存失败");
                }
            } else {
                return msgerror("用户信息解密失败:{$miniprogram->getErrCode}:{$miniprogram->getErrMsg()}");
            }
        } else {
            // return msgerror($validate->getError());
            return msgerror('参数缺失');
        }
    }
    //用户端-数据修改
    public function upload()
    {
        $this->checkLogin();
        $post = param();
        if(!empty($post)){
            $post['id'] = $this->userinfo['id'];
            unset($post['password']);
            unset($post['username']);
            $this->model::update($post);
            return datasuccess();
        }else{
            return msgerror('缺少参数');
        }
    }
    
}
